//
//  GKStatusCell.swift
//  MicroBlogFunc
//
//  Created by GK on 16/4/14.
//  Copyright © 2016年 GK. All rights reserved.
//

import UIKit
import SnapKit


class GKStatusCell: UITableViewCell {

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1)
        prepareUI()
    }
    
    var status: GKStatus?
    {
        didSet
        {   //将数据传递给cell 的 originView
            originalView.status = status
            
            //卸载掉bottomview的顶部约束
            bottomViewTopConstrain?.uninstall()
            
            if let retweetStatus = status?.retweeted_status
            {   // 如果有被转发的微博,将被转发的微博设置给被转发的view,bottomView的顶部参照被转发view的底部
                reTweetView.hidden = false
                reTweetView.retweetStatus = retweetStatus

                bottomView.snp_makeConstraints(closure: { (make) in
                    bottomViewTopConstrain = make.top.equalTo(reTweetView.snp_bottom).constraint
                })

//                GKPrint("转发转发转发\(retweetStatus)")
            }
            else
            {
                reTweetView.hidden = true
                
                bottomView.snp_makeConstraints(closure: { (make) in
                     bottomViewTopConstrain = make.top.equalTo(originalView.snp_bottom).constraint
                })
            }
            
            
        }
    }
    // 记录之前bottomview的约束
    var bottomViewTopConstrain: Constraint?
    
    //加载子控件
    private func prepareUI()
    {
        contentView.addSubview(originalView)
        contentView.addSubview(reTweetView)
        contentView.addSubview(bottomView)
        
        
        //添加约束
        originalView.snp_makeConstraints { (make) in
            make.leading.trailing.equalTo(contentView)
            make.top.equalTo(contentView).offset(10)
        }
        
        reTweetView.snp_makeConstraints { (make) in
            make.top.equalTo(originalView.snp_bottom)
            make.leading.trailing.equalTo(contentView)
            //TODO 高度
//            make.height.equalTo(200)
            
        }
        
        
        bottomView.snp_makeConstraints { (make) in
            make.leading.trailing.equalTo(contentView)
            make.height.equalTo(37)
            
            bottomViewTopConstrain =  make.top.equalTo(reTweetView.snp_bottom).constraint
        }
        
        // cell的高度根据里面子控件的约束来自动调整
        // 3.cell的子控件约束要正确,还需要约束contentView的约束(最主要的是高度约束),系统会让cell取contentView的高度
        contentView.snp_makeConstraints { (make) in
            
            make.leading.top.trailing.equalTo(self)
            make.bottom.equalTo(bottomView)
        }
        
    }
    
    
    
    

    //懒加载
    //cell 的顶部view
    private lazy var originalView: GKStatusOriginalView = GKStatusOriginalView()
    //被转发的cell
    private lazy var reTweetView: GKStatusRetweetView = GKStatusRetweetView()
    //cell 的底部view
    private lazy var bottomView: GKStatusBottomView = GKStatusBottomView()
    
    
}
